package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetmeal;
import com.itheima.service.OrderService;
import com.itheima.utils.SMSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Reference
    private OrderService orderService;
    @Autowired
    private JedisPool jedisPool;

    /**
     * 预约信息提交
     * @param map
     * @return
     */
    @RequestMapping("/submit")
    public Result submit(@RequestBody Map map) {
        //从页面获取手机号
        String telephone = (String) map.get("telephone");
        //从redis中获取验证码
        Jedis resource = jedisPool.getResource();
        String validateCodeInRedis = resource.get(telephone + RedisMessageConstant.SENDTYPE_ORDER);
        //归还资源
        resource.close();
        //从页面获取验证码
        String validateCode = (String) map.get("validateCode");
        //判断发送的验证码和输入的验证码是否相同
        if (validateCodeInRedis != null && validateCode != null && validateCodeInRedis.equals(validateCode)) {
            //如果对比相同，设置预约类型，分为微信预约、电话预约
            map.put("orderType", Order.ORDERTYPE_WEIXIN);
            Result result = null;
            try {
                //将页面的参数提交到业务层处理，完成预约处理
                result = orderService.order(map);
            } catch (Exception e) {
                e.printStackTrace();
                //如果预约处理失败，返回预约失败，而不是直接return，防止前台空指针
                return new Result(false, MessageConstant.ORDERSETTING_FAIL);
            }
            //预约成功，给客户发送预约成功短信
            if (result.isFlag()) {
                try {
                    SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE, telephone, (String) map.get("orderDate"));
                } catch (ClientException e) {
                    e.printStackTrace();
                }
            }
            return result;
        } else {
            //如果对比不成功，返回预约失败信息
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }
    }

    /**
     * 根据预约ID查询预约相关信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Map map = orderService.findById(id);
            //查询预约信息成功返回
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            //查询预约信息成功返回
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    /**
     * 查询指定体检人的预约信息
     * @param orderSetmeal
     * @return
     */
    @RequestMapping("/find4Detail")
    public Result ufind4Detail(@RequestBody OrderSetmeal orderSetmeal){
        try {
            String decode = URLDecoder.decode(orderSetmeal.getOrderName());
            orderSetmeal.setOrderName(decode);
            //查询预约信息成功返回
            // System.out.println(id);
            //System.out.println(orderName);
            String orderDate = orderSetmeal.getOrderDate().substring(0,10);
            orderSetmeal.setOrderDate(orderDate);
            System.out.println(orderSetmeal);
            Map<String,Object > map= orderService.findOrderAndSetmeal(orderSetmeal);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            //查询预约信息成功返回
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }
}
